#import "UmengAnalyticsPlugin.h"
#import "MobClick.h"
#import <Cordova/CDVPluginResult.h>

@implementation UmengAnalyticsPlugin

- (void)init:(CDVInvokedUrlCommand*)command
{
    NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];
    NSString* appKey = [infoDict objectForKey:@"UmengAppKey"];
    [MobClick startWithAppkey:appKey reportPolicy:BATCH   channelId:@"BAIDU"];
    [self successResponse:command];
}

- (void)setDebugMode:(CDVInvokedUrlCommand*)command
{
    [MobClick setLogEnabled:YES];
    [self successResponse:command];
}

- (void)onKillProcess:(CDVInvokedUrlCommand*)command
{
    [self successResponse:command];
}

- (void)onPageStart:(CDVInvokedUrlCommand*)command
{
    NSArray* args = command.arguments;
    NSString* pageName =args[0];
    [MobClick beginLogPageView:pageName];
    [self successResponse:command];
}

- (void)onPageEnd:(CDVInvokedUrlCommand*)command
{
    NSArray* args = command.arguments;
    NSString* pageName =args[0];
    [MobClick endLogPageView:pageName];
    [self successResponse:command];
}

- (void)onEvent:(CDVInvokedUrlCommand*)command
{
    NSArray* args = command.arguments;
    NSDictionary* dict = args[0];
    NSDictionary* content = [dict valueForKey:@"content"];
    NSString* eventId = [dict valueForKey:@"eventId"];
    if(content == nil)
    {
        [MobClick event:eventId];
    }
    else
    {
         [MobClick event:eventId attributes:content];
    }
    [self successResponse:command];
}

- (void)onEventValue:(CDVInvokedUrlCommand*)command
{
    NSArray* args = command.arguments;
    NSDictionary* dict = args[0];
    NSDictionary* content = [dict valueForKey:@"content"];
    NSString* eventId = [dict valueForKey:@"eventId"];
    NSNumber* value = [dict valueForKey:@"value"];
    [MobClick event:eventId attributes:content counter:[value intValue]];
    [self successResponse:command];
}

- (void)successResponse:(CDVInvokedUrlCommand*)command
{
    NSString* callbackId = command.callbackId;
    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:callbackId];
}


@end

